/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on May 9, 2011, 8:10:29 PM
 */
package battleship;

import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;

/**
 *
 * @author kamil
 */
@SuppressWarnings("serial")
public class MainFrame extends javax.swing.JFrame {
	protected NewGameSettings settings;
	protected Rules rules = new Rules();
	protected GamePhase gamePhase = GamePhase.START;

	/** Creates new form MainFrame */
	public MainFrame() {
		initComponents();
	}
	
	protected void clearValidateAndRepaintAll(){
		infoPanel.removeAll();
		infoPanel.validate();
		infoPanel.repaint();
		GameBoard.removeAll();
		GameBoard.validate();
		GameBoard.repaint();
	}
	void validateAndRepaintAll() {
		infoPanel.validate();
		infoPanel.repaint();
		GameBoard.validate();
		GameBoard.repaint();
	}
	protected void startGame(){
		Battleship battleshipGame = new Battleship(this, rules);
		battleshipGame.startGame();
	}
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        aboutDialog = new javax.swing.JDialog();
        aboutMainPanel = new javax.swing.JPanel();
        battleship2011Label = new javax.swing.JLabel();
        tomaszBinczyckiLabel = new javax.swing.JLabel();
        kamilWojtuchLabel = new javax.swing.JLabel();
        closeButton = new javax.swing.JButton();
        mainBoard = new javax.swing.JSplitPane();
        infoPanel = new javax.swing.JPanel();
        GameBoard = new Board(this);
        upperMenu = new javax.swing.JMenuBar();
        gameMenu = new javax.swing.JMenu();
        newGameButton = new javax.swing.JMenuItem();
        showSettingsButton = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        exitButton = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        aboutButton = new javax.swing.JMenuItem();

        aboutDialog.setTitle("about...");
        aboutDialog.setMinimumSize(new java.awt.Dimension(280, 280));
        aboutDialog.setModal(true);
        aboutDialog.setName("aboutDialog"); // NOI18N
        aboutDialog.setResizable(false);

        aboutMainPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        battleship2011Label.setFont(new java.awt.Font("Dialog", 1, 24));
        battleship2011Label.setText("Battleship 2011");

        tomaszBinczyckiLabel.setFont(new java.awt.Font("Dialog", 1, 14));
        tomaszBinczyckiLabel.setText("Tomasz Bińczycki");

        kamilWojtuchLabel.setFont(new java.awt.Font("Dialog", 1, 14));
        kamilWojtuchLabel.setText("Kamil Wojtuch");

        javax.swing.GroupLayout aboutMainPanelLayout = new javax.swing.GroupLayout(aboutMainPanel);
        aboutMainPanel.setLayout(aboutMainPanelLayout);
        aboutMainPanelLayout.setHorizontalGroup(
            aboutMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(aboutMainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(battleship2011Label)
                .addContainerGap(33, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, aboutMainPanelLayout.createSequentialGroup()
                .addContainerGap(134, Short.MAX_VALUE)
                .addComponent(kamilWojtuchLabel)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, aboutMainPanelLayout.createSequentialGroup()
                .addContainerGap(106, Short.MAX_VALUE)
                .addComponent(tomaszBinczyckiLabel)
                .addContainerGap())
        );
        aboutMainPanelLayout.setVerticalGroup(
            aboutMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(aboutMainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(battleship2011Label)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 71, Short.MAX_VALUE)
                .addComponent(tomaszBinczyckiLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(kamilWojtuchLabel)
                .addContainerGap())
        );

        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout aboutDialogLayout = new javax.swing.GroupLayout(aboutDialog.getContentPane());
        aboutDialog.getContentPane().setLayout(aboutDialogLayout);
        aboutDialogLayout.setHorizontalGroup(
            aboutDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(aboutDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(aboutDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(aboutMainPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(closeButton, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        aboutDialogLayout.setVerticalGroup(
            aboutDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(aboutDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(aboutMainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(closeButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        setTitle("Battleship 2011");
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        mainBoard.setDividerLocation(200);

        infoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Game Info"));
        infoPanel.setMaximumSize(new java.awt.Dimension(189, 32767));
        infoPanel.setMinimumSize(new java.awt.Dimension(189, 400));

        javax.swing.GroupLayout infoPanelLayout = new javax.swing.GroupLayout(infoPanel);
        infoPanel.setLayout(infoPanelLayout);
        infoPanelLayout.setHorizontalGroup(
            infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 188, Short.MAX_VALUE)
        );
        infoPanelLayout.setVerticalGroup(
            infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 375, Short.MAX_VALUE)
        );

        mainBoard.setLeftComponent(infoPanel);

        javax.swing.GroupLayout GameBoardLayout = new javax.swing.GroupLayout(GameBoard);
        GameBoard.setLayout(GameBoardLayout);
        GameBoardLayout.setHorizontalGroup(
            GameBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 367, Short.MAX_VALUE)
        );
        GameBoardLayout.setVerticalGroup(
            GameBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 402, Short.MAX_VALUE)
        );

        mainBoard.setRightComponent(GameBoard);

        gameMenu.setMnemonic('G');
        gameMenu.setText("Game");

        newGameButton.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
        newGameButton.setMnemonic('N');
        newGameButton.setText("New game");
        newGameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newGameButtonActionPerformed(evt);
            }
        });
        gameMenu.add(newGameButton);

        showSettingsButton.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        showSettingsButton.setText("Show Settings");
        showSettingsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showSettingsButtonActionPerformed(evt);
            }
        });
        gameMenu.add(showSettingsButton);
        gameMenu.add(jSeparator1);

        exitButton.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        exitButton.setMnemonic('x');
        exitButton.setText("Exit");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });
        gameMenu.add(exitButton);

        upperMenu.add(gameMenu);

        helpMenu.setMnemonic('H');
        helpMenu.setText("Help");

        aboutButton.setMnemonic('A');
        aboutButton.setText("About");
        aboutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutButtonActionPerformed(evt);
            }
        });
        helpMenu.add(aboutButton);

        upperMenu.add(helpMenu);

        setJMenuBar(upperMenu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainBoard, javax.swing.GroupLayout.DEFAULT_SIZE, 573, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainBoard, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
		int result;
		
		switch (this.gamePhase) {
			case START:
			case GAME_END:
				result = 0;
				break;
			default:
				result = JOptionPane.showConfirmDialog(this, "Are you sure?", "exiting...",
					JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		}
		
		if (result == 0)
			System.exit(0);
	}//GEN-LAST:event_exitButtonActionPerformed

	private void aboutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutButtonActionPerformed
//		 about dialog
		aboutDialog.setVisible(true);
	}//GEN-LAST:event_aboutButtonActionPerformed

	private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
		aboutDialog.setVisible(false);
	}//GEN-LAST:event_closeButtonActionPerformed

	private void newGameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newGameButtonActionPerformed
		clearValidateAndRepaintAll();
		for(Component c: settings.containers){
			if(c instanceof JRadioButton) continue;
			c.setEnabled(true);
		}
		settings.removeShipsButton.setEnabled(false);
		settings.clear();
		settings.setVisible(true);
		settings.toFront();
	}//GEN-LAST:event_newGameButtonActionPerformed

	private void showSettingsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showSettingsButtonActionPerformed
		settings.validate();
		settings.repaint();
		for(Component c: settings.containers){
			c.setEnabled(false);
		}
		settings.setVisible(true);
	}//GEN-LAST:event_showSettingsButtonActionPerformed

	private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
		int result;

		switch (this.gamePhase) {
			case START:
			case GAME_END:
				result = 0;
				break;
			default:
				result = JOptionPane.showConfirmDialog(this, "Are you sure?", "exiting...",
					JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		}

		if (result == 0)
			System.exit(0);
	}//GEN-LAST:event_formWindowClosing

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				MainFrame mainFrame = new MainFrame();
				mainFrame.settings = new NewGameSettings(mainFrame.rules, mainFrame);
				mainFrame.setVisible(true);
			}
		});
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected javax.swing.JPanel GameBoard;
    private javax.swing.JMenuItem aboutButton;
    private javax.swing.JDialog aboutDialog;
    private javax.swing.JPanel aboutMainPanel;
    private javax.swing.JLabel battleship2011Label;
    private javax.swing.JButton closeButton;
    private javax.swing.JMenuItem exitButton;
    private javax.swing.JMenu gameMenu;
    private javax.swing.JMenu helpMenu;
    protected javax.swing.JPanel infoPanel;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JLabel kamilWojtuchLabel;
    private javax.swing.JSplitPane mainBoard;
    private javax.swing.JMenuItem newGameButton;
    private javax.swing.JMenuItem showSettingsButton;
    private javax.swing.JLabel tomaszBinczyckiLabel;
    private javax.swing.JMenuBar upperMenu;
    // End of variables declaration//GEN-END:variables

}
